package com.pengshun.modules.api.pay.service;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author luoligang
 */
public class PayUtil {

    /**
     * 创建签名
     * @param params
     * @param secret
     * @return
     */
    public static String createSign(SortedMap<String, String> params, String secret) {
        StringBuffer sb = new StringBuffer();
        Set<?> es = params.entrySet();
        Iterator<?> it = es.iterator();
        while (it.hasNext()) {
            Map.Entry<?,?> entry = (Map.Entry<?,?>) it.next();
            String k = (String) entry.getKey();
            String v = (String) entry.getValue();
            if (null != v && !"".equals(v) && !"sign".equals(k) && !"key".equals(k)) {
                sb.append(k + "=" + v + "&");
            }
        }
        sb.append("key=" + secret);
        String sign = Md5Util.MD5Encode(sb.toString(),"UTF-8").toUpperCase();
        return sign;
    }

    /**
     * 创建支付随机字符串
     * @return
     */
    public static String getNonceStr(){
        return UUID.randomUUID().toString().replace("-", "");
    }

    /**
     * 生成订单号
     * @return
     */
    public static String getTradeNo() {
        String date = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
        Random r = new Random();
        return date+r.nextInt(10)+r.nextInt(10)+r.nextInt(10)+r.nextInt(10);
    }

    public static String notifyResult(){
        return "<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>";
    }

}

